package edu.umd.essic.maskbuilder;

import java.awt.geom.Point2D;

public class Dialog2D extends javax.swing.JDialog {

    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;
    public boolean OnOK;
    private Point2D.Float[] point_cen,  skew,  dim;
    private int numreg;
    private int returnStatus = RET_CANCEL;
    Object[][] data = new Object[][]{
        //xcenter          alpha_x         sigma_x       ycenter
        {new Float(-156), new Float(0.75), new Float(15), new Float(21), new Float(0.75), new Float(15)},
        {new Float(165), new Float(0.75), new Float(15), new Float(-20), new Float(0.75), new Float(15)},
        {new Float(0), new Float(0), new Float(0), new Float(0), new Float(0), new Float(0)},
        {new Float(0), new Float(0), new Float(0), new Float(0), new Float(0), new Float(0)},
        {new Float(0), new Float(0), new Float(0), new Float(0), new Float(0), new Float(0)}
    };
    Object[] header = new String[]{"x_bar", "alpha_x", "sigma_x", "y_bar", "alpha_y", "sigma_y"};

    /** Creates new form 2D_Dialog */
    public Dialog2D(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        skew = new Point2D.Float[5];
        dim = new Point2D.Float[5];
        point_cen = new Point2D.Float[5];
        for (int i = 0; i < 5; i++) {
            point_cen[i] = new Point2D.Float(0f, 0f);
            skew[i] = new Point2D.Float(0f, 0f);
            dim[i] = new Point2D.Float(0f, 0f);
        }
        initComponents();
        numreg = 1;
        String str = String.valueOf(numreg);
        Numreg.setText(str);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        Numreg = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        OKButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();

        jLabel1.setText("jLabel1");

        setTitle("Enter coefficients of 2D stretching");
        setFocusCycleRoot(false);
        setFont(new java.awt.Font("Arial", 0, 12));
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                CloseDialog(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridLayout(2, 1, 2, 5));

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setPreferredSize(new java.awt.Dimension(442, 190));
        jPanel4.setLayout(new java.awt.GridLayout(1, 0));

        jLabel2.setBackground(new java.awt.Color(255, 255, 255));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/umd/essic/maskbuilder/2dformula.jpg"))); // NOI18N
        jLabel2.setAlignmentY(0.0F);
        jLabel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel2.setIconTextGap(0);
        jLabel2.setMaximumSize(new java.awt.Dimension(500, 320));
        jLabel2.setMinimumSize(new java.awt.Dimension(405, 70));
        jLabel2.setPreferredSize(new java.awt.Dimension(440, 160));
        jPanel4.add(jLabel2);

        getContentPane().add(jPanel4);

        jPanel5.setPreferredSize(new java.awt.Dimension(442, 190));
        jPanel5.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 5, 2));

        jPanel1.setMaximumSize(new java.awt.Dimension(500, 36));
        jPanel1.setPreferredSize(new java.awt.Dimension(440, 32));
        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 2));

        jLabel4.setBackground(new java.awt.Color(255, 255, 255));
        jLabel4.setFont(new java.awt.Font("Book Antiqua", 2, 14));
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel4.setText("N (up to 5)");
        jLabel4.setPreferredSize(new java.awt.Dimension(100, 27));
        jPanel1.add(jLabel4);
        jLabel4.getAccessibleContext().setAccessibleName(" N (up to 5)");

        Numreg.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        Numreg.setName("Numreg"); // NOI18N
        Numreg.setPreferredSize(new java.awt.Dimension(80, 27));
        jPanel1.add(Numreg);

        jPanel5.add(jPanel1);

        jPanel2.setBorder(javax.swing.BorderFactory.createCompoundBorder());
        jPanel2.setMaximumSize(new java.awt.Dimension(500, 400));
        jPanel2.setPreferredSize(new java.awt.Dimension(440, 110));
        jPanel2.setLayout(null);

        jScrollPane1.setViewportBorder(javax.swing.BorderFactory.createCompoundBorder());
        jScrollPane1.setMaximumSize(new java.awt.Dimension(600, 300));
        jScrollPane1.setMinimumSize(new java.awt.Dimension(300, 50));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(440, 110));

        jTable1.setAutoCreateColumnsFromModel(false);
        jTable1.setBorder(javax.swing.BorderFactory.createCompoundBorder());
        jTable1.setModel(new javax.swing.table.DefaultTableModel(data, header));
        jTable1.setMaximumSize(new java.awt.Dimension(600, 500));
        jTable1.setMinimumSize(new java.awt.Dimension(200, 100));
        jTable1.setPreferredSize(new java.awt.Dimension(435, 100));
        jTable1.setRequestFocusEnabled(false);
        jScrollPane1.setViewportView(jTable1);

        jPanel2.add(jScrollPane1);
        jScrollPane1.setBounds(0, 0, 440, 110);

        jPanel5.add(jPanel2);

        jPanel3.setMaximumSize(new java.awt.Dimension(440, 40));
        jPanel3.setPreferredSize(new java.awt.Dimension(440, 34));

        OKButton.setFont(new java.awt.Font("MS Sans Serif", 0, 14));
        OKButton.setText("OK");
        OKButton.setMaximumSize(new java.awt.Dimension(100, 29));
        OKButton.setPreferredSize(new java.awt.Dimension(90, 27));
        OKButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        OKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKButtonActionPerformed(evt);
            }
        });
        jPanel3.add(OKButton);

        CancelButton.setFont(new java.awt.Font("MS Sans Serif", 0, 14));
        CancelButton.setText("Cancel");
        CancelButton.setMaximumSize(new java.awt.Dimension(90, 60));
        CancelButton.setMinimumSize(new java.awt.Dimension(90, 35));
        CancelButton.setPreferredSize(new java.awt.Dimension(90, 27));
        CancelButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });
        jPanel3.add(CancelButton);

        jPanel5.add(jPanel3);

        getContentPane().add(jPanel5);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        getRootPane().setDefaultButton(OKButton);
    }//GEN-LAST:event_formComponentShown

    private void CloseDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_CloseDialog
        doClose(RET_CANCEL);
    }//GEN-LAST:event_CloseDialog

    private boolean CheckValues() {
        boolean answer = true;
        float[][] values = new float[5][6];
        int Nmax;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 6; j++) {
                try {
                    values[i][j] = Float.parseFloat(jTable1.getValueAt(i, j).toString());
                } catch (NumberFormatException e) {
                    return false;
                }
            }
            if ((Math.abs(values[i][0]) > 360) || (Math.abs(values[i][3]) > 360)) {
                answer = false;
            }
        }
        try {
            Nmax = Integer.parseInt(Numreg.getText());
        } catch (NumberFormatException e) {
            return false;
        }
        if ((Nmax < 0) || (Nmax > 5)) {
            answer = false;
        }
        return answer;
    }

    private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed
        if (!CheckValues()) {
            javax.swing.JOptionPane.showMessageDialog(this, "Wrong value!", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
        } else {
            numreg = Integer.parseInt(Numreg.getText());
            for (int i = 0; i < numreg; i++) {
                point_cen[i].x = Float.parseFloat(jTable1.getValueAt(i, 0).toString());
                skew[i].x = Float.parseFloat(jTable1.getValueAt(i, 1).toString());
                dim[i].x = Float.parseFloat(jTable1.getValueAt(i, 2).toString());
                skew[i].y = Float.parseFloat(jTable1.getValueAt(i, 4).toString());
                dim[i].y = Float.parseFloat(jTable1.getValueAt(i, 5).toString());
                if (point_cen[i].x < 0) {
                    point_cen[i].x = 360 + point_cen[i].x;
                }
                point_cen[i].y = Float.parseFloat(jTable1.getValueAt(i, 3).toString());
            }
            OnOK = true;
            doClose(RET_OK);
        }
    }//GEN-LAST:event_OKButtonActionPerformed

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        // TODO add your handling code here:
        doClose(RET_CANCEL);
        OnOK = false;
    }//GEN-LAST:event_CancelButtonActionPerformed

    private void doClose(int retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
    }

    public Point2D.Float[] getSkew() {
        if (returnStatus == RET_OK) {
            return skew;
        } else {
            return null;
        }

    }

    public Point2D.Float[] getDim() {
        if (returnStatus == RET_OK) {
            return dim;
        } else {
            return null;
        }
    }

    public Point2D.Float[] getCenter() {
        if (returnStatus == RET_OK) {
            return point_cen;
        } else {
            return null;
        }
    }

    public int getNumreg() {
        if (returnStatus == RET_OK) {
            return numreg;
        } else {
            return 0;
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JTextField Numreg;
    private javax.swing.JButton OKButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
